package com.ruoyi.business.service.impl;

import java.sql.Array;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.ruoyi.business.domain.BizCoupon;
import com.ruoyi.business.domain.BizPeopleCouponExport;
import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BizPeopleCouponMapper;
import com.ruoyi.business.domain.BizPeopleCoupon;
import com.ruoyi.business.service.IBizPeopleCouponService;

/**
 * 用户券码Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-18
 */
@Service
public class BizPeopleCouponServiceImpl implements IBizPeopleCouponService 
{
    @Autowired
    private BizPeopleCouponMapper bizPeopleCouponMapper;

    /**
     * 查询用户券码
     * 
     * @param userCouponId 用户券码主键
     * @return 用户券码
     */
    @Override
    public BizPeopleCoupon selectBizPeopleCouponByUserCouponId(Long userCouponId)
    {
        return bizPeopleCouponMapper.selectBizPeopleCouponByUserCouponId(userCouponId);
    }

    /**
     * 查询用户券码列表
     * 
     * @param bizPeopleCoupon 用户券码
     * @return 用户券码
     */
    @Override
    @CommunityScope
    public List<BizPeopleCoupon> selectBizPeopleCouponList(BizPeopleCoupon bizPeopleCoupon)
    {
        return bizPeopleCouponMapper.selectBizPeopleCouponList(bizPeopleCoupon);
    }

    @Override
    @CommunityScope
    public List<BizPeopleCouponExport> selectBizPeopleCouponListExport(BizPeopleCoupon bizPeopleCoupon) {

        List<BizPeopleCouponExport> exports = new ArrayList<>();
        List<BizPeopleCoupon> bizPeopleCoupons = bizPeopleCouponMapper.selectBizPeopleCouponList(bizPeopleCoupon);
        LocalDateTime now = LocalDateTime.now();
        for (BizPeopleCoupon peopleCoupon : bizPeopleCoupons) {
            BizPeopleCouponExport export = new BizPeopleCouponExport();
            export.setPeopleName(peopleCoupon.getPeopleName());
            export.setCommunityName(peopleCoupon.getCommunityName());
            export.setTitle(peopleCoupon.getTitle());
            if (peopleCoupon.getStatus().equals(0l)) {
                export.setStatusText("待使用");
                if (peopleCoupon.getEnd().isBefore(now)) {
                    export.setStatusText("已过期");
                }
            }
            else if  (peopleCoupon.getStatus().equals(1l)){
                export.setStatusText("已使用");
            }
            else {export.setStatusText("已使用");}
            ///** 类型 0:满减,1:打折券,2:兑换券 */
            if (peopleCoupon.getCouponType().equals(0l)) {
                export.setCouponTypeText("满减券");
            }
            else if (peopleCoupon.getCouponType().equals(1l)) {
                export.setCouponTypeText("打折券");
            } else if (peopleCoupon.getCouponType().equals(2l)) {
                export.setCouponTypeText("兑换券");
            } else {}
            //规则
            String s = this.couponRule(peopleCoupon);
            export.setRule(s);
            export.setVerificationStore(peopleCoupon.getVerificationStore());
            export.setVerificationTime(peopleCoupon.getVerificationTime());
            export.setScore(peopleCoupon.getScore().toString());
            export.setVerificationStore(peopleCoupon.getVerificationStore());
            export.setVerificationCode(peopleCoupon.getVerificationCode());
            export.setReceivedTime(peopleCoupon.getReceivedTime());
            export.setStart(peopleCoupon.getStart());
            export.setEnd(peopleCoupon.getEnd());
            exports.add(export);
        }
        return exports;
    }

    /**
     * 新增用户券码
     * 
     * @param bizPeopleCoupon 用户券码
     * @return 结果
     */
    @Override
    public int insertBizPeopleCoupon(BizPeopleCoupon bizPeopleCoupon)
    {
        bizPeopleCoupon.setCreateTime(DateUtils.getNowDate());
        return bizPeopleCouponMapper.insertBizPeopleCoupon(bizPeopleCoupon);
    }

    /**
     * 修改用户券码
     * 
     * @param bizPeopleCoupon 用户券码
     * @return 结果
     */
    @Override
    public int updateBizPeopleCoupon(BizPeopleCoupon bizPeopleCoupon)
    {
        bizPeopleCoupon.setUpdateTime(DateUtils.getNowDate());
        return bizPeopleCouponMapper.updateBizPeopleCoupon(bizPeopleCoupon);
    }

    /**
     * 批量删除用户券码
     * 
     * @param userCouponIds 需要删除的用户券码主键
     * @return 结果
     */
    @Override
    public int deleteBizPeopleCouponByUserCouponIds(Long[] userCouponIds)
    {
        return bizPeopleCouponMapper.deleteBizPeopleCouponByUserCouponIds(userCouponIds);
    }

    /**
     * 删除用户券码信息
     * 
     * @param userCouponId 用户券码主键
     * @return 结果
     */
    @Override
    public int deleteBizPeopleCouponByUserCouponId(Long userCouponId)
    {
        return bizPeopleCouponMapper.deleteBizPeopleCouponByUserCouponId(userCouponId);
    }

    /**
     * 查询用户券的数量
     *
     * @param status
     * @param start
     * @param end
     * @return
     */
    @Override
    @CommunityScope(alias = "")
    public Long selectTotalByStatus(BizCoupon bizCoupon) {
        return bizPeopleCouponMapper.selectTotalByStatus(bizCoupon);
    }

    /**
     * 已过期的券总数
     * @param bizCoupon
     * @return
     */
    @Override
    @CommunityScope(alias = "")
    public Long expiredTotal(BizCoupon bizCoupon) {
        return bizPeopleCouponMapper.expiredTotal(bizCoupon);
    }
    public static String couponRule(BizPeopleCoupon coupon) {
        // todo 1-券有效期 有效期类型 0 :自领取日多少天内有效,1:开始&结束
        StringBuffer sb = new StringBuffer();
        // 类型 0:满减,1:打折券,2:兑换券
        if (coupon.getCouponType().equals(0l)) {
            sb.append("满"+coupon.getAmount()+"元，减"+coupon.getReduce()+"元");
        }
        else if (coupon.getCouponType().equals(1l)) {
            sb.append("满"+coupon.getAmount()+"元，打"+coupon.getDiscount()+"折");
        }
        else if(coupon.getCouponType().equals(2l)){
            sb.append("满"+coupon.getAmount()+"元，兑换领取"+coupon.getExchangeItems());
        }
        else {

        }
        return sb.toString();
    }
}
